package com.czk.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * 拿到spring的上下文对象
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/27 17:19
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 拿到前端控制器
     *
     * @return
     */
    public static DispatcherServlet getServlet() {
        return applicationContext.getBean(DispatcherServlet.class);
    }
}
